package com.example.base.demo.configuration;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.context.annotation.Profile;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author TangDan
 * @version 1.0
 * @since 2023/3/10
 */
public class CustomMovieEditor extends PropertyEditorSupport implements PropertyEditorRegistrar {

    @Override
    public String getAsText() {
        return  LocalDateTime.now().toString();
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            Date date = new SimpleDateFormat("yyyyMMdd").parse(String.valueOf(text));
            LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
            setValue(localDateTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry) {
        registry.registerCustomEditor(LocalDateTime.class,this);
    }
}
